﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ScannerNS;
using ScannerNS.Classes;

namespace ScannerNS.Classes
{
   public class Scanner : IScanner
    {
        private string fileContent;
        public string reducedString;
        private int charCounter = 0;
        private List<string> tokens = new List<string>(); 

        public Scanner(string fileName)
        {
            FileReader fr = new FileReader();
            fileContent = fr.readFile(fileName);
            buildTokenList();
        }

        private bool isLetter(char c)
        {
            return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z';
        }

        private bool isDigit(char c)
        {
            return c >= '0' && c <= '9';
        }

        private bool isSeparator(char c)
        {
            if (c == ' ' || c == '\r' || c == '\n' || c == '\t')
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public void buildTokenList()
        {
            while (hasChar())
            {
                tokens.Add(getToken());
            }
        }

        public string getToken()
        {
            char tempChar = ' ';
            string token = "";

            while (hasChar())
            {
                tempChar = getChar();

                if (!isSeparator(tempChar))
                {
                    if (isLetter(tempChar) || isDigit(tempChar))
                    {
                        token += tempChar;
                    }
                }

                else
                {
                    break;
                }
            }

            return token;

            //for (int i = 0; i < fileContent.Length; i++)
            //{
            //    reducedString = fileContent.Substring(i);

            //    Console.WriteLine(reducedString);
            //}
           
        }
        
        private bool hasChar()
        {
            return charCounter<fileContent.Length;
        }

        public char getChar()
        {
           char tempChar = fileContent[charCounter];
           charCounter++;
           return tempChar;
        }

        public string Scan()
        {
            string token = tokens.First();
            tokens.Remove(token);

            return token;

        }

        public bool HasNext()
        {
            return tokens.Count > 0;
        }









    }
}
